<?php 
namespace Common\Model;

use Org\Wechat\WechatAuth;
/**
 * 订单处理modal
 * @author lanxuebao
 *
 */
class OrderModel extends BaseModel{
    protected $tableName = 'mall_trade';
    
    public function buyNum($goods_id, $buyer_id){
        $sql = "SELECT SUM(num) FROM mall_order_product WHERE buyer_id='{$buyer_id}' AND goods_id='{$goods_id}'";
        $result = $this->query($sql);
        $total = current($result[0]);
        return $total ? $total : 0;
    }
    
    /**
     * 生成微信支付(*total_fee单位为元，系统会自动转成分)
     * @param unknown $data
     * @param string $retry
     * @return \Org\WxPay\成功时返回，其他抛异常|\Org\WxPay\json数据，可直接填入js函数作为参数
     */
    public function createWxPayOrder($data, $retry = false){
        //下单结果通知url
        if(!isset($data['notify_url']))
	        $data['notify_url'] = U('/service/wxpaynotify', '', true, true);
	    
	    ini_set('date.timezone','Asia/Shanghai');
	    
	    $tools = new \Org\WxPay\JsApiPay();

	    //②、统一下单
	    $input = new \Org\WxPay\WxPayUnifiedOrder();
	    $input->SetBody($data['body']);
	    $input->SetDetail($data['detail']);
	    $input->SetAttach($data['attach']);
	    $input->SetOut_trade_no($data['order_no']);
	    $input->SetTotal_fee($data['total_fee'] * 100);
	    $input->SetTime_start($data['time_start']);
	    $input->SetTime_expire($data['time_expire']);
	    $input->SetGoods_tag($data['goods_tag']);
	    $input->SetNotify_url($data['notify_url']);
	    $input->SetSpbill_create_ip(get_client_ip());
	    $input->SetTrade_type(IS_APP ? 'APP' : 'JSAPI');
	    $input->SetOpenid($data['openid']);
	    $order = \Org\WxPay\WxPayApi::unifiedOrder($input);
	    
	    // 保存微信订单记录
	    $values = $input->GetValues();
	    $data = array_merge($values, $order);
	    M('wx_order')->add($data);
	    
	    if($order['result_code'] != 'SUCCESS'){
	        if($retry == true){
	            $this->createWxPayOrder($data, false);
	        }else{
	            return $order;
	        }
	    }
	    
	    return $tools->GetParameters($order);
    }
    
    /**
     * 订单支付超时处理
     * @param unknown $trade
     */
    public function timeout(&$trade){
        if($trade['status'] != 'topay' || $trade['pay_type'] == 'giveaway' || strtotime($trade['created']) + C('ORDER_TIME_OUT') > NOW_TIME){
            return;
        }
        
        $this->startTrans();
        
        // 退款
        $this->cancelTrade($trade, 'pay_timeout');
        
        $this->commit();
    }
    
    /**
     * 订单统一处理入口
     * @param unknown $trade
     */
    public function handle(&$trade){
        $static = D('Common/Static');
        //判定是否超时
        $this->timeout($trade);
        
        // 订单状态
        $statusList = $static->orderStatus();
        if($trade['pay_type'] == 'giveaway'){
            if($trade['status'] == 'topay'){
                $trade['status_str'] = '未领取';
            }else if($trade['status'] == 'cancel'){
                $trade['status_str'] = '已弃领';
            }else{
                $trade['status_str'] = $statusList[$trade['status']];
            }
        }else{
            $trade['status_str'] = $statusList[$trade['status']];
        }
        $aliStatuses = $static->aliStatus();
        $trade['ali_status_str'] = $aliStatuses[$trade['astatus']];
        $trade['pay_type_str'] = $static->payType($trade['pay_type']);
        
        $trade['sum_fee'] = sprintf('%.2f', $trade['total_fee'] + $trade['post_fee'] - $trade['discount_fee'] - $trade['paid_no_balance'] + $trade['adjust_fee']);
        
        // 退款状态
        $refundState = $static->refundedState();;
        foreach($trade['orders'] as $i=>$item){
            $trade['orders'][$i]['spec'] = get_spec_name($item['sku_json']);
            $trade['orders'][$i]['price'] = $item['price'];
            $trade['orders'][$i]['original_price'] = $item['original_price'] > 0 ? $item['original_price'] : '';
            $trade['orders'][$i]['refund_state_str'] = empty($item['refund_id']) ? '无退款' : $refundState[$item['refund_state']];
            $status_str = '';
            switch ($item['status']){
                case 'tosend':
                    $status_str = '待发货';
                    break;
                case 'send':
                    $status_str = '已发货';
                    break;
            }
            
            $trade['orders'][$i]['status_str'] = $status_str;
        }
    }
    
    public function getShortList($where, $limit = 0, $offset = 20){
        $list = $this->alias("trade")
                ->field("trade.tid, trade.paid_fee, trade.created, trade.kind, trade.status, trade.pay_type, trade.type, trade.post_fee, trade.payment, trade.paid_no_balance, `order`.oid, `order`.goods_id, trade.buyer_id,
	                    `order`.title, `order`.pic_url, `order`.price, `order`.original_price, `order`.pay_type AS ppt, `order`.num, `order`.sku_json")
               ->join("mall_order AS `order` ON `order`.oid=trade.tid")
               ->where($where)
               ->order("trade.created DESC")
               ->limit($limit, $offset)
               ->select();
        $data = array();

        // 字段处理
        foreach($list as $index=>$item){
            if(!isset($data[$item['tid']])){
                $this->handle($item);
                $data[$item['tid']] = array(
                    'tid'       => $item['tid'],
                    'type'      => $item['type'],
                    'created'   => $item['created'],
                    'status'    => $item['status'],
                    'status_str'=> $item['status_str'],
                    'post_fee'  => $item['post_fee'],
                    //'receiver_name'   => $item['receiver_name'],
                    //'receiver_mobile'   => $item['receiver_mobile'],
                    'kind'      => $item['kind'],
                    'payment'   => $item['payment'],
                    'pay_type'  => $item['pay_type'],
                    'orders'    => array()
                );
            }

            $data[$item['tid']]['orders'][] = array(
                'oid' => $item['oid'],
                'goods_id'  => $item['goods_id'],
                'product_id'=> $item['product_id'],
                'title' => $item['title'],
                'num' => $item['num'],
                'original_price' => $item['original_price'],
                'price' => $item['price'],
                'pic_url' => $item['pic_url'],
                'pay_type' => $item['ppt'],
                'spec' => get_spec_name($item['sku_json'])
            );
        }
        
        return array_values($data);
    }
    
    /**
     * 取消订单
     * @param unknown $tid 订单号
     */
    public function cancel($tid, $buyer_id){
        $result = array('error' => 0, 'msg' => '已退款');
        
        $trade = $this->where("tid='%s'", $tid)->find();
        if(empty($trade) || $buyer_id != $trade['buyer_id']){
            $result['error'] = 1;
            $result['msg'] = '订单不存在';
        }

        $this->handle($trade);
        if($trade['status'] == 'cancel'){
            return $result;
        }
        
        if($trade['status'] != 'topay'){
            $result['error'] = 1;
            $result['msg'] = '订单已确认，无法取消';
            return $result;
        }
        
        // 退款
        $this->cancelTrade($trade, 'buyer_cancel', true);
        return $result;
    }
    
    /**
     * 订单取消后 扣除受益人差价提成
     */
    private function backDiff($trade, $sendMsg = false){
        $diff = $this->query("SELECT SUM(profit.total_fee) AS total_fee, profit.mid
                              FROM mall_trade_difference AS profit
                              WHERE profit.tid = '{$trade['tid']}' AND profit.checkout=1
                              GROUP BY tid");
        if(empty($diff)){
            return 0;
        }
        $diff = $diff[0];
        
        // 扣除上级的差价收益
        D('Balance')->add(array(
            'mid'       => $diff['mid'],
            'reason'    => '好友取消订单['.$trade['tid'].']',
            'balance'   => -$diff['total_fee'],
            'link'      => '/h5/order/detail?tid='.$trade['tid'],
            'type'      => 'lower_cancel'
        ));

        // 发送消息
        if($sendMsg){
            return $diff['total_fee'];
        }

        $myWX = $this->getWXUserConfig($diff['mid']);
        if(empty($myWX['config'])){
            return $diff['total_fee'];
        }

        ignore_user_abort(true);
        set_time_limit(0);
        $url = '/h5/order/detail?tid='.$trade['tid'];
        //发送模板消息
        $WechatAuth = new WechatAuth($myWX['config']['WEIXIN']);
        $WechatAuth->sendTemplate($myWX['openid'], array(
            'template_id' => $myWX['config']['WX_TEMPLATE']['TM00335'],
            'url' => $myWX['config']['HOST'].$url,
            'data' => array(
                'first'    => array('value' => '您有新积分扣除，详情如下。', 'color' => '#173177'),
                'account'  => array('value' => '当前账户'),
                'time'     => array('value' => date('Y年m月d日 H:i')),
                'type'     => array('value' => '好友订单被取消'),
                'creditChange' => array('value' => '扣除'),
                'number'       => array('value' => sprintf('%.2f', $diff['total_fee'])),
                'creditName'   => array('value' => '积分'),
                'amount'       => array('value' => '***'),
                'remark'       => array('value' => '好友订单'.$trade['tid'].'已售后退款，故积分应返还给好友')
            )
        ));
        
        return $diff['total_fee'];
    }
    
    /**
     * 返还订单金额
     * @param unknown $trade
     * @param unknown $reason
     */
    public function cancelTrade(&$trade, $reason, $sendMsg = false){
        ignore_user_abort(true);
        set_time_limit(0);
        
        $now = date('Y-m-d H:i:s');
        $trade['status'] = 'cancel';
        $trade['end_type'] = $reason;
        $trade['end_time'] = $now;
        $trade['modified'] = $now;
        
        // 退款折扣率
        $rate = 1;
        if($reason == 'buyer_cancel'){
            $rate = 0.99;
        }
        
        if($reason != 'pay_timeout'){
            $sendMsg = true;
        }
        
        // 返还金额
        $refundedFee = sprintf('%.2f', $trade['paid_fee'] * $rate);
        $refundState = 'no_refund';
        
        // 更新订单信息
        $sql = "UPDATE mall_trade SET status='cancel', end_time='".$now."', end_type='{$reason}'";
        if($trade['paid_fee'] > 0){
            $refundState = $refundedFee == $trade['paid_fee'] ? 'full_refunded' : 'partial_refunded';
            $sql .= ", refund_state='{$refundState}', refunded_fee='{$refundedFee}'";
        }
        $sql .= " WHERE tid='{$trade['tid']}'";
        $this->execute($sql);
        
        // 删除退款申请
        if($trade['paid_fee'] > 0){
            $this->execute("DELETE FROM mall_trade_refund WHERE refund_id IN (SELECT oid FROM mall_order WHERE tid={$trade['tid']})");
        }
        
        // 将钱退回到个人钱包
        if($refundedFee > 0){
            $noBalance = $trade['paid_no_balance'] >= $refundedFee ? $refundedFee : $trade['paid_no_balance'];
            D('Balance')->add(array(
                'mid'       => $trade['buyer_id'],
                'reason'    => '订单退款-'.$trade['tid'],
                'no_balance'=> $noBalance,
                'balance'   => $refundedFee - $noBalance,
                'link'      => '/h5/order/detail?tid='.$trade['tid'],
                'type'      => 'order_refunded'
            ));
            
            if($sendMsg){
                $myWX = $this->getWXUserConfig($trade['buyer_id']);
                if(!empty($myWX['config'])){
                    $url = '/h5/order/detail?tid='.$trade['tid'];
                    //发送模板消息
                    $WechatAuth = new WechatAuth($myWX['config']['WEIXIN']);
                    $WechatAuth->sendTemplate($myWX['openid'], array(
                        'template_id' => $myWX['config']['WX_TEMPLATE']['TM00335'],
                        'url' => $myWX['config']['HOST'].$url,
                        'data' => array(
                            'first'    => array('value' => '您有新积分到账，详情如下。', 'color' => '#173177'),
                            'account'  => array('value' => empty($trade['buyer_nick']) ? '当前账户' : $trade['buyer_nick']),
                            'time'     => array('value' => date('Y年m月d日 H:i')),
                            'type'     => array('value' => '订单退款'),
                            'creditChange' => array('value' => '增加'),
                            'number'       => array('value' => $refundedFee),
                            'creditName'   => array('value' => '积分'),
                            'amount'       => array('value' => '***'),
                            'remark'       => array('value' => '订单'.$trade['tid'].'已取消')
                        )
                    ));
                }
            }
        }
        
        //订单取消后 扣除受益人 和 平台获取的利益
        $this->backDiff($trade, $sendMsg);
        
        $trade['refund_state'] = $refundState;
        $trade['refunded_fee'] = $refundedFee;
    }
    
    /**
     * 确认收货(签收)
     * @param unknown $tid
     * @param unknown $buyer
     * @return multitype:number string
     */
    public function sign($tid, $buyer_id){
        $static = D('Common/Static');
        $trade = $this->where("tid='%s'", $tid)->find();
        if(empty($trade) || $buyer_id != $trade['buyer_id']){
            $this->error = '订单不存在';
            return 0;
        }
    
        $this->handle($trade);
        
        if($trade['status'] != 'send'){
            $this->error = '当前不可签收：'.$static->orderStatus($trade['status']);
            return 0;
        }
    
        $now = date('Y-m-d H:i:s');
        $this->where("tid='{$tid}'")->save(array(
            'status'    => 'success',
            'sign_time' => $now,
            'end_time'  => $now,
            'modified'  => $now
        ));
        
        return 1;
    }
    
    public function delete($tid, $buyer_id){
        $this->execute("UPDATE ".$this->tableName." SET buyer_del=1 WHERE tid='%s' AND buyer_id='%d'", array($tid, $buyer_id));
    }
    
    public function getTradeByTid($tid){
        $trade = $this->where("tid='%s'", $tid)->find();
        if(empty($trade)){
            $this->error = '订单不存在';
            return null;
        }
        
        // 如果是1688的订单
        if($trade['type'] == '1688' && in_array($trade['status'], array('toout', 'send', 'success'))){
            $AM = new AlibabaModel();
            $update = $AM->getAliTrade($trade['tid']);
            if(isset($update[$trade['tid']])){
                foreach ($update[$trade['tid']] as $field=>$value){
                    if(isset($trade[$field])){
                        $trade[$field] = $value;
                    }
                }
            }
        }

        // 订单详情
        $trade['orders'] = $this
            ->query("SELECT `order`.*, refund.*,difference.total_fee,difference.checkout,difference.mid
                     FROM mall_order AS `order`
                     LEFT JOIN mall_trade_refund AS refund ON refund.refund_id=`order`.oid
                     LEFT JOIN mall_trade_difference AS difference ON difference.oid=`order`.oid
                     WHERE `order`.tid='{$tid}'");
        
        // 查找物流信息
        $trade['express'] = array();
        $staticM = D('Common/Static');
        if($trade['express_no'] == ''){
            $express = $staticM->express(false, 'id');
            $trade['express_name'] = $express[$trade['express_id']]['name'];
        }else if(strpos($trade['express_no'], ':')){  // 快递公司名称:运单号
            $express = $staticM->express(false, 'name');
            $expressList = explode(';', $trade['express_no']);
            foreach($expressList as $item){
                $detail = explode(':', $item);
                $trade['express'][] = array('name' => $detail[0], 'code' => $express[$detail[0]]["code"], 'no' => $detail[1]);
            }
        }else{   // 直接运单号
            $express = $staticM->express();
            foreach($express as $k=>$v){
                if($trade['express_id'] == $v["id"]){
                    $trade['express'][] = array('name' => $v["name"], 'code' => $v["code"], 'no' => $trade['express_no']);
                    break;
                }
            }
        }
        
        $this->handle($trade);
        return $trade;
    }
    
    /**
     * 获取销售数量 - 根据买家ID
     * @param unknown $goodsId
     * @param unknown $buyerId
     * @return Ambigous <number, mixed>
     */
    public function getSoldNumByBuyer($goodsId, $buyerId){
        $sql = "SELECT SUM(o.num) FROM mall_order AS o 
                INNER JOIN mall_trade AS t ON t.tid=o.tid
                WHERE t.buyer_id='{$buyerId}' AND o.goods_id='{$goodsId}'
                  AND t.status != 'cancel'";
        
        $result = $this->query($sql);
        $total = current($result[0]);
        return $total ? $total : 0;
    }
    
    /**
     * 获取销售数量 - 根据时间
     * @param unknown $goodsId
     * @param unknown $startTime
     * @param unknown $endTime
     * @return Ambigous <number, mixed>
     */
    public function getSoldNumByTime($goodsId, $startTime, $endTime){
        $sql = "SELECT SUM(o.num) FROM mall_order AS o 
                INNER JOIN mall_trade AS t ON t.tid=o.tid
                WHERE o.goods_id='{$goodsId}'
                  AND t.status != 'cancel' AND t.status != 'topay'
                  AND t.created BETWEEN '{$startTime}' AND '{$endTime}'";
        
        $result = $this->query($sql);
        $total = current($result[0]);
        return $total ? $total : 0;
    }
    
    /**
     * 获取销售数量 - 根据买家时间
     * @param unknown $goodsId
     * @param unknown $buyerId
     * @param unknown $startTime
     * @param unknown $endTime
     * @return Ambigous <number, mixed>
     */
    public function getSoldNumByBuyerTime($goodsId, $buyerId, $startTime, $endTime){
        $sql = "SELECT SUM(o.num) FROM mall_order AS o
                INNER JOIN mall_trade AS t ON t.tid=o.tid
                WHERE t.buyer_id='{$buyerId}' AND o.goods_id='{$goodsId}'
                AND t.status != 'cancel'
                AND t.created BETWEEN '{$startTime}' AND '{$endTime}'";
        
        $result = $this->query($sql);
        $total = current($result[0]);
        return $total ? $total : 0;
    }
    
    /**
     * 设置收货地址
     * @param unknown $param
     */
    public function setReceiver($tid, $address) {
          $this->where("tid='{$tid}'")->save($address);
    }
    
    /**
     * 验证订单是否可以取消(取消主订单)
     * @param unknown $trade 订单
     * @param unknown $time 当前时间
     */
    public function can_cancel($trade){
        if($trade['status'] == 'cancel'){
            $this->error = '当前状态不可取消！';
            return -1;
        }
        
        //是否有取消订单的权限
        $auth = \Common\Common\Auth::get();
        $accessCancel = $auth->validated('admin','order','cancel');
        
        if(!$accessCancel){
            $this->error = '无权限！';
            return -1;
        }
        
        //取消其他店铺的权限
        if($trade['seller_id'] != session('user.shop_id')){
            $accessAll = $auth->validated('admin','shop','all');
            if(!$accessAll){
                $this->error = '无权限！';
                return -1;
            }
        }
        
        if(($trade['status'] == 'topay' || $trade['status'] == 'tosend' || $trade['status'] == 'toout') && $trade['refunded_fee'] == 0){
            return 1;
        }
        
        // 是否有退款权限
        $access_cancel = $auth->validated('admin','refund','agree');
        if($access_cancel){
            return 2;
        }
        
        return -1;
    }
    
    /**
     * 校验产品
     */
    protected function validOrder($buyer, &$product, &$quotaList, $autoChange = false){
        $result = array('error_code' => 1, 'error_msg' => '');
    
        if($product['is_del']){
            $result['error_msg'] = '产品已被删除';
        }else if($product['is_display'] == 0){
            $result['error_msg'] = '产品已下架';
        }else if($product['sold_time'] > 0 && $product['sold_time'] > time()){
            $result['error_msg'] = date('Y-m-d H:i:s', $product['sold_time']).'开售';
        }else if($product['stock'] == 0){
            $result['error_msg'] = '已售罄';
        }else if($product['day_quota'] > 0 && !empty($quotaList[$product['goods_id']]['today_sold']) && $quotaList[$product['goods_id']]['today_sold'] >= $product['day_quota']){
            $result['error_msg'] = '今日已售罄';
        }else if($buyer['agent_level'] == 0 && $product['visitors_quota'] != 1){
            $result['error_msg'] = '非会员不可购买';
        }
    
        if($result['error_msg'] != ''){
            return $result;
        }
    
        if(intval($product['buy_num']) < 1){
            $result['error_msg'] = '不能小于1件';
            if(!$autoChange){ return $result; }
            $product['buy_num'] = 1;
        }
    
        if($product['buy_num'] > $product['stock']){
            $result['error_msg'] = '仅剩'.$product['stock'].'件';
            if(!$autoChange){ return $result; }
            $product['buy_num'] = $product['stock'];
        }
    
        $startTime = date('Y-m-d').' 00:00:00';
        $endTime = date('Y-m-d').' 23:59:59';
        $goodsId = $product['goods_id'];
    
        if(!isset($quotaList[$goodsId])){
            //$quotaList['商品id'] = array('今日卖出数量' => 0, '今日购买数量' => 0, '累计购买数量' => 0 );
            $quotaList[$goodsId] = array('today_sold' => -1, 'today_buy' => -1, 'buy_num' => -1);
        }
    
        // 每人每日最多可购买
        if($product['every_quota'] > 0){
            if($quotaList[$goodsId]['today_buy'] == -1){
                $quotaList[$goodsId]['today_buy'] = $this->getSoldNumByBuyerTime($goodsId, $buyer['id'], $startTime, $endTime);
            }
            $canBuy = $product['every_quota'] - $quotaList[$goodsId]['today_buy'];
    
            if($product['buy_num'] > $canBuy){
                $result['error_msg'] = '日限购'.$product['every_quota'].'件';
                if($canBuy > 0){
                    $result['error_msg'] .= '(可购'.$canBuy.'件)';
                    if(!$autoChange){ return $result; }
                    $product['buy_num'] = $canBuy;
                }else{
                    return $result;
                }
            }
            $quotaList[$goodsId]['today_buy'] += $product['buy_num'];
        }
    
        // 每日最多可售卖数量限制
        if($product['day_quota'] > 0){
            // 获取今日此商品卖出数量
            if($quotaList[$goodsId]['today_sold'] == -1){
                $quotaList[$goodsId]['today_sold'] = $this->getSoldNumByTime($goodsId, $startTime, $endTime);
            }
            $canSold = $product['day_quota'] - $quotaList[$goodsId]['today_sold'];
    
            if($product['buy_num'] > $canSold){
                $result['error_msg'] = '日限卖'.$product['day_quota'].'件';
                if($canSold > 0){
                    $result['error_msg'] .= '(可购'.$canSold.'件)';
                    if(!$autoChange){ return $result; }
                    $product['buy_num'] = $canSold;
                }else{
                    return $result;
                }
            }
            $quotaList[$goodsId]['today_sold'] += $product['buy_num'];
            $product['stock'] -= $product['buy_num'];
        }
    
        // 每人最多可购买数量
        if($product['buy_quota'] > 0){
            if($quotaList[$goodsId]['buy_num'] == -1){
                $quotaList[$goodsId]['buy_num'] = $this->getSoldNumByBuyer($product['goods_id'], $buyer['id']);
            }
            $canBuy = $product['buy_quota'] - $quotaList[$goodsId]['buy_num'];
    
            if($product['buy_num'] > $canBuy){
                $result['error_msg'] = '每人限购'.$product['buy_quota'].'件';
                if($canBuy > 0){
                    $result['error_msg'] .= '(可购'.$canBuy.'件)';
                    if(!$autoChange){ return $result; }
                    $product['buy_num'] = $canBuy;
                }else{
                    return $result;
                }
            }
            $quotaList[$goodsId]['buy_num'] += $product['buy_num'];
        }
    
        if($product['buy_num'] > $product['stock']){
            $result['error_msg'] = '仅剩'.$product['stock'].'件';
            if(!$autoChange){ return $result; }
            $product['buy_num'] = $product['stock'];
        }
    
        $result['error_code'] = 0;
        return $result;
    }
    
        
    /**
     * 订单备注
     */
    public function sendOne($data){
        $Model = M();
        $sql = "SELECT
                mall_trade.tid,
                mall_trade.receiver_name,
                mall_trade.receiver_mobile,
                mall_trade.receiver_province,
                mall_trade.receiver_city,
                mall_trade.receiver_county,
                mall_trade.receiver_detail,
                mall_trade.buyer_openid,
                mall_trade.`status`,
                mall_trade.express_id,
                mall_trade.express_no,
                wx_user.appid,
                wx_user.subscribe
                FROM
                mall_trade
                LEFT JOIN wx_user ON wx_user.openid = mall_trade.buyer_openid
                WHERE
                mall_trade.tid =".$data['tid'];
        $trade = $Model->query($sql);
        //$trade = $Model->query("SELECT tid, type, `status`, express_id, express_no FROM mall_trade WHERE tid =".$data['tid']);
        $result = array();
        if ( count($trade)>0 )
        {
            $trade = $trade[0];
            if ( strlen($trade['express_no'])>0 )
            {
                $res = $Model->execute("UPDATE mall_trade SET express_no='{$data['send']}' WHERE tid='{$data['tid']}'");
                $result[$data['tid']]['express_no'] = $data['send'];
                if($trade['express_no']!=$data['send']){
                    $send = explode(';',$data['send']);
                    foreach ($send as $value){
                        $express = explode(':',$value);
                        $express_name[] = $express[0];
                        $express_no[] = $express[1];
                    }
                    
                    if(count($express_name)>1 && count($express_no)>1){
                        $express_name = implode(' ',$express_name);
                        $express_no = implode(' ',$express_no);
                    }else{
                        $express_name = $express[0];
                        $express_no = $express[1];
                    }
                    
                    $config = get_wx_config($trade['appid']);
                    $WechatAuth = new WechatAuth($config['WEIXIN']);
                    $message = array(
                    'template_id' => $config['WX_TEMPLATE']['OPENTM200565259'],
                    'url' => $config['HOST'].'/h5/order/detail?tid='.$trade['tid'],
                    'data' => array(
                        "first"    => array("value" => '亲,您的物流信息有变动'),
                        "keyword1" => array("value" => $trade['tid']),
                        "keyword2" => array("value" => $express_name),
                        "keyword3" => array("value" => $express_no),
                        "remark"   => array("value" => '收获信息：'."\t".$trade["receiver_name"] ."\t".$trade["receiver_mobile"]."\t".$trade["receiver_province"]. $trade["receiver_city"]. $trade["receiver_county"]. $trade["receiver_detail"])
                    )
                    );
                        $WechatAuth->sendTemplate($trade['buyer_openid'], $message);
                } 
                
            }else{
                $date = date("Y-m-d H:i:s");
                $res = $Model->execute("UPDATE mall_trade SET `status`=IF(`status`='toout', 'send', `status`),express_no='{$data['send']}',`consign_time`=IF(ISNULL(consign_time), '{$date}', `consign_time`) WHERE tid='{$data['tid']}'");
                if ($trade['status'] == 'toout') $result[$data['tid']]['status'] = 'send';
                if ($trade['consign_time'] == '') $result[$data['tid']]['consign_time'] = $date;
                $result[$data['tid']]['express_no'] = $data['send'];
            }
            return $result;
        }
        return false;
    }
    
    /**
     * 获取交易中的用户信息
     * @param array $login 登录session标记
     */
    public function getTradingBuyer($login){
         $sql = "SELECT member.id, member.pid, member.nickname, wx.openid, wx.subscribe, member.sex,
                    member.mobile, member.province_id, member.city_id, member.county_id, member.address AS detail,
                    member.balance, member.no_balance, member.agent_level, IF(member.pid>0, (SELECT pm.agent_level FROM member AS pm WHERE pm.id=member.pid), 0) AS parent_level
                FROM member, wx_user AS wx
                WHERE member.id={$login['id']} AND wx.openid='{$login['openid']}'
                LIMIT 1";
        
        $buyer = $this->query($sql);
        if(empty($buyer)){
            session('[destroy]');
            //E('登陆账号异常，请重新关注后再试！');
        }
        return $buyer[0];
    }


    /**
     * 获取交易中的用户信息
     * @param array $login 登录session标记
     */
    public function getTradingProducts($id){
        $sql = "SELECT
                    product.id, product.goods_id, goods.score, goods.points, GROUP_CONCAT(tag.tag_id) AS tags,
                    goods.title, goods.pic_url AS goods_pic_url, product.pic_url, product.price,goods.points, goods.original_price,
                    goods.pay_type, goods.is_virtual, goods.cat_id, goods.buy_quota, goods.day_quota, goods.every_quota,
                    product.stock, product.outer_id, product.sku_json, product.weight, product.cost,
                    goods.sold_time, goods.is_del, goods.is_display, goods.visitors_quota, goods.freight_tid,
                    product.agent2_price, product.agent3_price, goods.shop_id AS seller_id, shop.name AS seller_nick
                FROM
                    mall_product AS product
                INNER JOIN mall_goods AS goods ON goods.id = product.goods_id
                INNER JOIN shop ON shop.id = goods.shop_id
                LEFT JOIN mall_goods_tag AS tag ON tag.goods_id=goods.id
                WHERE product.id IN(".rtrim($id, ',').")
                GROUP BY product.id
                ORDER BY product.id";
        
        return $this->query($sql);
    }
}
?>